package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.ProductDTO;
import net.wanho.entity.Product;
import net.wanho.service.ProductService;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.ProductVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
public class ProductController {
    private final ProductService productService;

    /**
     *  商品列表
     */
    @GetMapping("/product-page-list")
    public AjaxResult productPageList(ProductDTO productDTO,
                                      @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        return AjaxResult.success(productService.productPageList(productDTO, pageNum, pageSize));
    }

    /**
     *  添加商品
     */
    @PostMapping("/product-add")
    public AjaxResult productAdd(@RequestBody Product product) {
        boolean isSave = productService.save(product);
        if (!isSave) {
            return AjaxResult.error("商品添加失败！");
        }
        return AjaxResult.success(null, "商品添加成功！");
    }

    /**
     *  根据Id查询商品
     */
    @GetMapping("/{id}")
    public AjaxResult productById(@PathVariable("id") Integer id) {
        Product product = productService.getById(id);
        if (product == null) {
            return AjaxResult.error("商品不存在！");
        }
        return AjaxResult.success(product);
    }

    /**
     *  编辑商品信息
     */
    @PutMapping("/product-update")
    public AjaxResult productUpdate(@RequestBody ProductVo productVo) {
        productService.updateProductById(productVo);
        return AjaxResult.success(null, "商品修改成功！");
    }

    /**
     *  删除商品信息
     */
    @DeleteMapping("/product-delete/{id}")
    public AjaxResult productDelete(@PathVariable Integer id) {
        productService.removeProductById(id);
        return AjaxResult.success(null, "商品删除成功！");
    }

    /**
     *  批量删除商品
     */
    @DeleteMapping("/product-list-delete")
    public AjaxResult productListDelete(@RequestBody List<Integer> idList) {
        productService.removeProductBatchByIds(idList);
        return AjaxResult.success(null, "商品批量删除成功！");
    }

    /**
     *  商品导出
     */
    @GetMapping("/exportTable")
    public AjaxResult exportTable(ProductDTO productDTO,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        return AjaxResult.success(productService.productPageList(productDTO, pageNum, pageSize).getResultList());
    }

    /**
     *  上架/下架
     */
    @PutMapping("/state-change")
    public AjaxResult updateProductState(@RequestBody ProductVo productVo) {
        productService.updateProductById(productVo);
        return AjaxResult.success(null, "修改成功！");
    }
}
